const itembank = require('../../utils/itembank.js');
var app = getApp();
Page({
  data: {
    subjects: [],
    myResult: [],
    currentResult: [],
    current: 1,
    buttonText: "下一题",
    checked: false,
    unchecked: false
  },
  /**
   * 选项变化事件
   */
  radioChange: function(e) {
    var currentResult = this.data.currentResult
    var value = e.detail.value //选项值
    if (currentResult.length == this.data.current) { //若果是修改值
      currentResult.pop()
      currentResult.push(value)
    } else { //如果是第一次选择
      currentResult.push(value)
    }
    this.setData({//更新当下结果
      checked: true,
      currentResult: currentResult
    });
    //将当下结果存入缓存
    wx.setStorage({
      key: "currentResult",
      data: currentResult
    })
    wx.setStorage({
      key: "current",
      data: this.data.current
    })
  },
  onLoad: function(option) {
    var currentResult = wx.getStorageSync('currentResult');
    var current = wx.getStorageSync('current');
    if (!currentResult){
      currentResult = [];
    }
    if (!current) {
      current = 1;
    }
    var subjects = itembank.getSubjectData();
    this.setData({
      subjects: subjects,
      currentResult: currentResult,
      current: current
    });
  },

  /**
   * 下一题（或提交）按钮事件
   */
  next: function(e) {
    if (!this.data.checked) { //如果本题还没有选择，不能进行下一题
      return
    }
    var total = this.data.subjects.length //总的题目数量
    if (this.data.current == total) { //当前若是最后一题时，提交结果
      var currentResult = this.data.currentResult;
      //app.globalData.result = result;
      wx.setStorage({//将测试选项结果保存到缓存中
        key: "myResult",
        data: currentResult
      })
      wx.setStorage({//将缓存中的当下题目编号置1
        key: "current",
        data: 1
      })
      wx.setStorage({//清空缓存中的当下题目结果
        key: "currentResult",
        data: []
      })
      wx.navigateTo({//跳转到报告页
        url: '/pages/report/report',
      })
    } else if (this.data.current == total - 1) { //当前若是倒数第二题时，进入最后一题时改变按钮文字样式为“提交”
      this.setData({
        buttonText: "提交",
        current: this.data.current + 1,
        checked: !this.data.checked,
        unchecked: false
      });
    } else { //进入下一题
      this.setData({
        current: this.data.current + 1,
        checked: !this.data.checked,
        unchecked: false
      });
    }

  }

})